package com.ruoyi.internet.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.internet.domain.IntService;
import com.ruoyi.internet.mapper.IntServiceMapper;
import com.ruoyi.internet.service.IIntServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务模块Service业务层处理
 *
 * @author cjie
 * @date 2021-08-16
 */
@Service
public class IntServiceServiceImpl implements IIntServiceService {
    @Autowired
    private IntServiceMapper intServiceMapper;

    /**
     * 查询服务模块
     *
     * @param id 服务模块ID
     * @return 服务模块
     */
    @Override
    public IntService selectIntServiceById(String id) {
        IntService intService = intServiceMapper.selectIntServiceById(id);
        if(StringUtils.isNotEmpty(intService.getPicsUrl())){
            intService.setPicsUrlArray(JSON.parseArray(intService.getPicsUrl()));
        }
        return intService;
    }

    /**
     * 查询服务模块列表
     *
     * @param intService 服务模块
     * @return 服务模块
     */
    @Override
    public List<IntService> selectIntServiceList(IntService intService) {
        List<IntService> intServices = intServiceMapper.selectIntServiceList(intService);
        intServices.forEach(service->{
            if(StringUtils.isNotEmpty(service.getPicsUrl())){
                service.setPicsUrlArray(JSON.parseArray(service.getPicsUrl()));
            }
        });
        return intServices;
    }

    /**
     * 新增服务模块
     *
     * @param intService 服务模块
     * @return 结果
     */
    @Override
    public int insertIntService(IntService intService) {
        intService.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        intService.setCreateTime(DateUtils.getNowDate());
        intService.setCreateBy(SecurityUtils.getUsername());
        if(intService.getPicsUrlArray() != null){
            intService.setPicsUrl(JSON.toJSONString(intService.getPicsUrlArray()));
        }
        return intServiceMapper.insertIntService(intService);
    }

    /**
     * 修改服务模块
     *
     * @param intService 服务模块
     * @return 结果
     */
    @Override
    public int updateIntService(IntService intService) {
        intService.setUpdateTime(DateUtils.getNowDate());
        intService.setUpdateBy(SecurityUtils.getUsername());
        if(intService.getPicsUrlArray() != null){
            intService.setPicsUrl(JSON.toJSONString(intService.getPicsUrlArray()));
        }
        return intServiceMapper.updateIntService(intService);
    }

    /**
     * 批量删除服务模块
     *
     * @param ids 需要删除的服务模块ID
     * @return 结果
     */
    @Override
    public int deleteIntServiceByIds(String[] ids) {
        return intServiceMapper.deleteIntServiceByIds(ids);
    }

    /**
     * 删除服务模块信息
     *
     * @param id 服务模块ID
     * @return 结果
     */
    @Override
    public int deleteIntServiceById(String id) {
        return intServiceMapper.deleteIntServiceById(id);
    }
}
